import fs from 'fs'
// 读取txt文件
const txtData = fs.readFileSync('out.txt', 'utf-8')

const csvData = parseText(txtData)

fs.writeFileSync('output.csv', csvData)

function parseText(text) {
    const result = ['学校代码,学校名称,录取人数'];

    // 正则表达式分隔学校数据
    const schoolPattern = /\d{4}[^\d]*?(?:大学|学院)[^地址]*?(?:地址：)/g;
    const schoolParts = text.match(schoolPattern)

    // 遍历每个学校数据部分
    for (let i = 0; i < schoolParts.length; i ++) {
        const schoolPart = schoolParts[i];
        if(schoolPart.indexOf('民办')>-1){
                continue
        }

        // 提取学校数据
        const schoolCodeMatch = schoolPart.match(/^(\d{4})/);
        const schoolNameMatch = schoolPart.match(/^\d{4}([^\d]*?(?:大学|学院))/);
        const regex = /(\d+)(?=[^\d]*地址：)/;

// 提取匹配的数字
        const total = text.match(regex);
        const schoolCode = schoolCodeMatch[1];
        const schoolName = schoolNameMatch[1].trim();
        result.push(`${schoolCode},${schoolName},${total}`);
    

    }

    return result.join('\n');
}
